package com.avantis.admin.service.impl;

import com.avantis.admin.service.CommonCodeService;
import com.avantis.common.base.BaseServiceImpl;
import com.avantis.common.entity.CommonCode;
import com.avantis.common.entity.query.CommonCodeQuery;
import com.avantis.common.repository.CommonCodeRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: tracywang
 * @Date: 2019/1/16
 * @Descrption:
 */
@Service
@Transactional
public class CommonCodeServiceImpl extends BaseServiceImpl<CommonCodeRepository,CommonCode> implements CommonCodeService {

    @Autowired
    private CommonCodeRepository commonCodeRepository;

    @Override
    public List<CommonCode> findCommonCodesByCodeType(String codeType) {
        return commonCodeRepository.findCommonCodesByCodeTypeAndParentId(codeType,"");
    }

    @Override
    public Page<CommonCode> findCommonCodeCriteria(CommonCodeQuery commonCodeQuery) {
        Pageable pageable = new PageRequest(commonCodeQuery.getPageNumber()-1,commonCodeQuery.getPageSize());
        Page<CommonCode> customerPage = commonCodeRepository.findAll(new Specification<CommonCode>() {

            @Override
            public Predicate toPredicate(Root<CommonCode> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(StringUtils.isNotEmpty(commonCodeQuery.getCodeType())) {
                    predicates.add(criteriaBuilder.like(root.get("codeType").as(String.class), "%"+commonCodeQuery.getCodeType()+"%"));
                }
                if(StringUtils.isNotEmpty(commonCodeQuery.getCodeTypeDesc())) {
                    predicates.add(criteriaBuilder.like(root.get("codeTypeDesc").as(String.class), "%"+commonCodeQuery.getCodeTypeDesc()+"%"));
                }
                predicates.add(criteriaBuilder.equal(root.get("parentId"),""));
                criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
                return criteriaQuery.getRestriction();
            }
        },pageable);

        //查询子类
        if(!CollectionUtils.isEmpty(customerPage.getContent())){
            for(CommonCode commonCode:customerPage.getContent()){
                commonCode.setList(commonCodeRepository.findCommonCodesByCodeTypeAndParentId(commonCode.getCodeType(),commonCode.getId()));
            }
        }
        return customerPage;
    }

    @Override
    public void saveCommonCode(CommonCode commonCode) {
        commonCodeRepository.save(commonCode);
    }
}
